package com.pengcheng.taskapplication.mvp.view.activity;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.pengcheng.taskapplication.R;
import com.pengcheng.taskapplication.contants.ConstantValue;
import com.pengcheng.taskapplication.model.SubscribeOrderModel;
import com.pengcheng.taskapplication.mvp.presenter.UserPresenter;
import com.pengcheng.taskapplication.net.IView;
import com.pengcheng.taskapplication.rxbus.RxBus;
import com.pengcheng.taskapplication.rxbus.RxBusRoute;
import com.pengcheng.taskapplication.rxbus.RxEvent;
import com.pengcheng.taskapplication.util.ToastUtils;

import java.io.IOException;

import butterknife.BindView;
import butterknife.OnClick;
import okhttp3.ResponseBody;

/**
 * Created by ZFT on 2020/10/28.
 */

public class ApplySuccessActivity extends BaseActivity implements IView{

    @BindView(R.id.tv_center_title)
    TextView tv_center_title;

    @BindView(R.id.tv_plate)
    TextView tv_plate;
    @BindView(R.id.tv_time)
    TextView tv_time;
    @BindView(R.id.tv_code)
    TextView tv_code;
    @BindView(R.id.img_qr)
    ImageView img_qr;
    private long id;
    public String contentEtString="测试";

    private UserPresenter userPresenter;
    private String code;

    @Override
    protected int layoutResID() {
        return R.layout.activity_apply_success_layout;
    }

    @Override
    protected void initData() {
        tv_center_title.setText("电子车牌");
        userPresenter = new UserPresenter(this);
        code=getIntent().getStringExtra("code");
        getSubscribeOrder(code);
        getQrCode(code,1024);
    }


    @Override
    public void onSuccess(String code, String msg, Object data, String actionType) {
        if(actionType.equals(ConstantValue.RequestKey.getSubscribeOrder)){
            SubscribeOrderModel model=(SubscribeOrderModel)data;
            tv_plate.setText(model.getPlateNo());
            tv_code.setText("工单号："+model.getCode());
            tv_time.setText("申请办理日期："+model.getSubscribeDt());
            id=model.getId();

        }
        if(actionType.equals(ConstantValue.RequestKey.cancelapply)){
            ToastUtils.showShortToast(msg);
            ApplySuccessActivity.this.finish();
        }
        if(actionType.equals(ConstantValue.RequestKey.getQrCode)){
            ResponseBody value=(ResponseBody)data;
            byte[] cache = new byte[0];
                try {
                    cache = value.bytes(); //注意：把byte[]转换为bitmap时，也是耗时操作，也必须在子线程
                    Bitmap bitmap = BitmapFactory.decodeByteArray(cache, 0, cache.length);
                    runOnUiThread(new Runnable() {//开启主线程更新UI
                        @Override
                        public void run() {
                            //设置图片到imageview
                            img_qr.setImageBitmap(bitmap);
                        }
                    });
                } catch (IOException e) {
                    e.printStackTrace();
                }

        }


    }

    @Override
    public void onFailed(String code, String msg) {

    }

    @OnClick({R.id.img_back,R.id.tv_goto_index,R.id.tv_cancel_apply})
    public void onViewClicked(View view) {
        Intent intent;
        switch (view.getId()) {
            case R.id.img_back:
                finish();
                break;
            case R.id.tv_goto_index:
                finish();
                break;
            case R.id.tv_cancel_apply:
                userPresenter.cancelApply(id,ConstantValue.RequestKey.cancelapply);
                break;
        }
    }
    public void getSubscribeOrder(String code){
        userPresenter.getSubscribeOrder(code,ConstantValue.RequestKey.getSubscribeOrder);
    }
    public  void getQrCode(String code,int size){
        userPresenter.getQrCode(code,size,ConstantValue.RequestKey.getQrCode);
    }
    //销毁的同时通知刷新
    @Override
    protected void onDestroy() {
        RxBus.getInstance().post(new RxEvent(RxBusRoute.REFRESH));
        super.onDestroy();
    }
}
